<template>
	<div>
		<el-row>
			<div class="crumbs">
				<el-breadcrumb separator="/">
					<el-breadcrumb-item><i class="el-icon-lx-news"></i> 新闻管理</el-breadcrumb-item>
				</el-breadcrumb>
			</div>
		</el-row>
		<div style="padding: 10px 0;background-color:#fff;">
			<edit-form @onSubmit="loadNews()" ref="edit"></edit-form>
			<el-card style="margin: 18px 2%;width: 95%">
				<el-table
						:data="tableData"
						border
						style="width: 100%">
					<el-table-column
							prop="id"
							label="id"
							style="text-align: center"
							width="50">
					</el-table-column>
					<el-table-column
							prop="title"
							label="新闻标题"
							width="330">
					</el-table-column>
					<el-table-column
							prop="date"
							label="日期"
							width="130">
					</el-table-column>
					<el-table-column
							prop="author"
							label="作者/来源"
							width="80">
					</el-table-column>
					<el-table-column
							prop="url"
							label="新闻链接"
							width="270">
					</el-table-column>
					
					<el-table-column
							prop="abs"
							label="简介"
							width="80">
					</el-table-column>
					<el-table-column
							prop="cover"
							label="图片"
							width="100">
					</el-table-column>
					<el-table-column
							fixed="right"
							label="操作"
					>
						<template slot-scope="scope">
							<!--<el-button v-on:click="edit(scope.row)" type="text" size="small">
								<update-form v-on:click="edit(scope.row)"></update-form>
							</el-button>-->
							<el-button v-on:click="edit(scope.row)" type="text" size="small">编辑</el-button>
							<el-button v-on:click="deleteIt(scope.row)" type="text" size="small">删除</el-button>
						</template>
					</el-table-column>
				</el-table>
				<el-pagination
						background
						style="margin: 10px auto 0; text-align: center"
						layout="prev, pager, next"
						:page-size="pageSize"
						:total="total"
						@current-change="page">
				</el-pagination>
			</el-card>
		</div>
	</div>
</template>

<script>
    import EditForm from './AddForm';
     import UpdateForm from './UpdateForm';

    export default {
        name: 'index',
        components: {
            EditForm,
			UpdateForm
        },
        data () {
            return {
                tableData: [],
                //multipleSelection: [],
                pageSize: 1,
                total: 11
            }
        },
        created () {
            const _this = this
            this.$http
                .get('/news/findAll/0/6')
                .then(function (resp) {
                    console.log(resp)
                    _this.tableData = resp.data.content
                    _this.pageSize = resp.data.size
                    _this.total = resp.data.totalElements
                })
        },
        methods: {
            //删除
            deleteIt (row) {
                this.$confirm('此操作将永久删除该条新闻, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                        const _this = this;
                        this.$http
							.delete('/news/deleteById/' + row.id).then(function (resp) {
                            _this.$alert('成功删除！！新闻：'+row.title , '消息', {
                                confirmButtonText: '确定',
                                callback: action => {
                                    window.location.reload()
                                }
                            })
                        })
                    }
                ).catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取消删除'
                    })
                })
            },
            // 编辑
            edit (row) {
                this.$router.push({
                    path: '/data/update',
                    query: {
                        id: row.id
                    }
                })
                console.log(row);
            },
            //绑定当前页数
            page (currentPage) {
                const _this = this
                this.$http.get('/news/findAll/' + (currentPage - 1) + '/6')
                    .then(function (resp) {
                        console.log(resp)
                        _this.tableData = resp.data.content
                        _this.pageSize = resp.data.size
                        _this.total = resp.data.totalElements
                    })
            }
        }
    }
</script>